//
//  EMLoginVC.swift
//  easeIM-swift
//
//  Created by yangjian on 2022/7/12.
//

import UIKit
import SnapKit
import RxSwift
import RxCocoa
import HyphenateChat

class EMLoginVC: UIViewController {

    lazy var bag = DisposeBag()
    
    
    
    lazy var backView : UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(red: 0 / 255.0, green:  0 / 255.0, blue:  0 / 255.0, alpha: 0.3)
        return view
    }()
    
    lazy var titleImageView : UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFill
        iv.image = UIImage(named: "titleImage")
        return iv
    }()
    
    lazy var usernameTF : UITextField = {
        let textField = UITextField()
        textField.placeholder = "username"
        return textField
    }()
    lazy var passwordTF : UITextField = {
        let textField = UITextField()
        textField.placeholder = "password"
        return textField
    }()

    lazy var loginButton : UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("login", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: .bold)
        return button
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.config()
    }
    func config(){
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(titleImageView)
        self.view.addSubview(usernameTF)
        self.view.addSubview(passwordTF)
        self.view.addSubview(loginButton)
        
        titleImageView.snp.makeConstraints { make in
            make.top.equalTo(96)
            make.width.equalTo(80)
            make.height.equalTo(80)
        }
        usernameTF.snp.makeConstraints { make in
            make.top.equalTo(self.titleImageView.snp.bottom).offset(20)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(40)
        }
        passwordTF.snp.makeConstraints { make in
            make.top.equalTo(self.usernameTF.snp.bottom).offset(20)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(40)
        }
        loginButton.snp.makeConstraints { make in
            make.top.equalTo(self.passwordTF.snp.bottom).offset(20)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.equalTo(40)
        }
        loginButton.rx.controlEvent(.touchUpInside).subscribe(onNext: {[unowned self] element in
            self.login()
        }) .disposed(by: bag)
        
        let usernameAndPasswordObserable = Observable.combineLatest(usernameTF.rx.text, passwordTF.rx.text)
        
        
        usernameAndPasswordObserable.subscribe { username,password in
            if username!.count > 0 , password!.count > 0 {
                self.loginButton.isEnabled = true
            }else{
                self.loginButton.isEnabled  = false
            }
        }
        .disposed(by: bag)
        
        let p =
        PublishSubject.combineLatest(usernameTF.rx.text, passwordTF.rx.text)
            
        p.subscribe { usernameElement,passwordElement in
            print("~~~~~~~~~~~~~~~~~~~~~\(usernameElement!)---\(passwordElement!)");
        }.disposed(by: bag)
        
        
        
        
        
        usernameTF.text = TEST_DEF_USERNAME
        passwordTF.text = TEST_DEF_PASSWORD
        usernameTF.sendActions(for: .valueChanged)
        passwordTF.sendActions(for: .valueChanged)
        

    }
}


extension EMLoginVC {
    func login(){
        
        let username = self.usernameTF.text!
        let password = self.passwordTF.text!
        
        EMClient.shared().login(withUsername: username, token: password) {username, error in
            
            //根据demo来的,省略其他所有内容,直接奔向主界面.
            if error == nil {
                //登录成功
                //demo中也是发送通知,这里也是发送通知
                RXEMObservableManager.share.clientObservable.rx_loginState.onNext(.login)
//                IMClientHelper.share.isLoginSub.onNext(.login)
            }else{
                //登录失败,做一些处理,这里直接省略
            }
        }
        
        return
        EMClient.shared().login(withUsername: username, password: password) { username, error in
            //根据demo来的,省略其他所有内容,直接奔向主界面.
            if error == nil {
                //登录成功
                //demo中也是发送通知,这里也是发送通知
                RXEMObservableManager.share.clientObservable.rx_loginState.onNext(.login)
//                IMClientHelper.share.isLoginSub.onNext(.login)
            }else{
                //登录失败,做一些处理,这里直接省略
            }
        }
    }
}
